package cn.chengpengper.rpc.proxy.cglib;

import cn.chengpengper.rpc.proxy.api.BaseProxyFactory;
import cn.chengpengper.rpc.proxy.api.ProxyFactory;
import cn.chengpengper.rpc.spi.annotation.SpiClass;
import lombok.extern.slf4j.Slf4j;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;

/**
 * @author chengpeng.hu
 */
@Slf4j
@SpiClass
public class CglibProxyFactory<T> extends BaseProxyFactory<T> implements ProxyFactory {

    private final Enhancer enhancer = new Enhancer();

    @Override
    public <T> T getProxy(Class<T> clazz) {
        log.info("基于CGLib动态代理...");
        enhancer.setInterfaces(new Class[]{clazz});
        enhancer.setCallback((InvocationHandler) (o, method, objects) -> objectProxy.invoke(o, method, objects));
        return (T) enhancer.create();

    }
}
